package risk.control.application.utils.data;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import risk.control.application.utils.mmkv.MMKVUtils;
import risk.control.application.utils.mmkv.MMKV_KEY;

public class TokenUtils {

    /**
     * 获取本地存储的token
     *
     * @return token
     */
    public static String get() {
        try {
            //获取本地用户信息
            String userInfoData = MMKVUtils.getString(MMKV_KEY.KV_USER_INFO_DATA);
            if (userInfoData == null || userInfoData.isEmpty()) {
                return "";
            }
            //解析数据
            JSONObject jsonObject = (JSONObject) JSON.parse(userInfoData);
            JSONObject data = jsonObject.getJSONObject("data");
            if (data == null || data.isEmpty()) {
                return "";
            }
            String token = data.getString("token");
            if (token == null || token.isEmpty()) {
                //跳转登录页面
                return "";
            }
            return token;
        } catch (Exception e) {
            return "";
        }
    }
}
